package com.study.hadoop.mapreduce.partition;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


/**
 * @Author huang
 * @Date 2022/1/3 3:12
 * @Version 1.0
 */
public class FlowReducer extends Reducer<Text, FlowBean,Text, FlowBean> {
    private FlowBean flowBean = new FlowBean();
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Reducer<Text, FlowBean, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        //初始化值
        Long downFlow=0l;
        Long upFlow=0l;
        //求和
        for (FlowBean value : values) {
            downFlow += value.getDownFlow();
            upFlow += value.getUpFlow();
        }
        //设值
        flowBean.setDownFlow(downFlow);
        flowBean.setUpFlow(upFlow);
        flowBean.setSumFlow();
        //写入
        context.write(key,flowBean);
    }
}
